<?php

/*

Plugin Name: Goodreads

Plugin URI: http://code.google.com/p/goodreadsplugin/downloads/list

Description: Displays info from your Goodreads account.  This includes text and images via RSS feed.  

Version: 1.1

License: GPL

Author: Cal Margulis

Author URI: http://margulis.net

*/



// display goodreads info

function goodreads($number_books = 10, $feed= 'read', $display_type = 'both', $image_size = 'small', $before = '<li>', $after = '</li>', $goodreads_id='153650') {



        // use configuration if args not set

        for($i = 0 ; $i < func_num_args(); $i++) {

                $args[] = func_get_arg($i);

        }

        if (!isset($args[0])) $number_books = get_option('goodreads_number_books'); 

        if (!isset($args[1])) $feed = get_option('goodreads_feed');

        if (!isset($args[2])) $display_type = trim(get_option('goodreads_display_type'));

        if (!isset($args[3])) $image_size = get_option('goodreads_image_size');

        if (!isset($args[4])) $before = stripslashes(get_option('goodreads_before'));

        if (!isset($args[5])) $after = stripslashes(get_option('goodreads_after'));

        if (!isset($args[6])) $goodreads_id = stripslashes(get_option('goodreads_goodreads_id'));



        // rss functions

        require_once(ABSPATH.'wp-includes/rss-functions.php');



                        $url = "http://www.goodreads.com/review/list_rss/".$goodreads_id."?shelf=".$feed;





        // url setup properly

        if ($url) {

        	

                $rss = fetch_rss($url);

                foreach (array_slice($rss->items,0,$number_books) as $item) {

                	

                        // vars

                        $title = $item['title'];

                        $link = $item['link'];

                        $image = $item['book_image_url'];





/* Image Size */



                        $smallimage = $image;

                        $largeimage = str_replace("-s-", "-m-", $smallimage);





                        // diplay type

   // diplay type



	               $display = '';

                        if(($display_type=='image') || ($display_type=='both')) {

                                        if ($image_size == 'small')

                                            $imgsource = $smallimage;

                                        else 

                                            $imgsource = $largeimage;

                                        $display = $display . '<img src="'.$imgsource.'" alt="'.$title.'" title="'.$title.'" />';

                        }

                        if(($display_type=='title') || ($display_type=='both')) {

                                        $display = $display . $title . '<br/>';

                        }



                        // display link

                        echo wptexturize($before.'<a href="'.$link.'">'.$display.'</a>'.$after);

                }

        }

}



// wigitized

function widget_myGoodreads() {

  ?> <h2 class="widgettitle">Goodreads</h2> <?php

   goodreads();

}



function myGoodreads_init()

{

  register_sidebar_widget(__('Goodreads'), 'widget_myGoodreads');     

}

add_action("plugins_loaded", "myGoodreads_init");







// subpanel to set goodreads options

function goodreads_subpanel() {

        // form submitted

        if (isset($_POST['configure_goodreads'])) {

                // gather form data

                $form_goodreads_id = $_POST['goodreads_id'];

                $form_number_books = $_POST['number_books'];

                $form_feed = $_POST['feed'];

                $form_display_type = $_POST['display_type'];

                $form_image_size = $_POST['image_size'];

                $form_before = $_POST['before'];

                $form_after = $_POST['after'];



                // update options

                update_option('goodreads_goodreads_id', $form_goodreads_id);

                update_option('goodreads_number_books', $form_number_books);

                update_option('goodreads_feed', $form_feed);

                update_option('goodreads_display_type', $form_display_type);

                update_option('goodreads_image_size', $form_image_size);

                update_option('goodreads_before', $form_before);

                update_option('goodreads_after', $form_after);

?>



<div class="updated">

  <p>Options changes saved.</p>

</div>

<?php

        }

?>

<div class="wrap">

  <h2>Goodreads Configuration</h2>

  <form method="post">

    <fieldset class="options">

    <table>

      <tr>

        <td><p><strong>

            <label for="flickr_nsid">Goodreads ID</label>

            :</strong></p></td>

        <td><input name="goodreads_id" type="text" id="goodreads_id" value="<?php echo get_option('goodreads_goodreads_id'); ?>" size="8" />

    

          </p></td>

      </tr>







      <tr>

        <td><p><strong>Feed:</strong></p></td>

        <td><select name="number_books" id="number_books">

        <?php for ($i = 1; $i <= 10; $i++) {?>

            <option <?php if(get_option('goodreads_number_books') == $i) { echo "selected"; } ?> value="<?php echo $i;?>"><?php echo $i;?></option>

        <?php } ?>

          </select>

          books from your



<input name="feed" type="text" id="feed" value="<?php echo get_option('goodreads_feed'); ?>" size="40" />



          shelf </td>

      </tr>



      <tr>

        <td><p><strong>Display:</strong> </p></td>

        <td><select name="display_type" id="display_type">

            <option <?php if(get_option('goodreads_display_type') == 'title') { echo "selected"; } ?> value="title">Book Title</option>

            <option <?php if(get_option('goodreads_display_type') == 'image') { echo "selected"; } ?> value="image">Cover Image</option>

            <option <?php if(get_option('goodreads_display_type') == 'both') { echo "selected"; } ?> value="both">Image and Title</option>

          </select>

          (if image select size:

          <select name="image_size" id="image_size">

            <option <?php if(get_option('goodreads_image_size') == 'small') { echo "selected"; } ?> value="small">Small</option>

            <option <?php if(get_option('goodreads_image_size') == 'large') { echo "selected"; } ?> value="large">Large</option>

          </select>

          ) 

      </tr>

      <tr>

        <td><p><strong>

            <label for="before">Before</label>

            /

            <label for="after">After</label>

            :</strong></p></td>

        <td><input name="before" type="text" id="before" value="<?php echo htmlspecialchars(stripslashes(get_option('goodreads_before'))); ?>" size="10" />

          /

          <input name="after" type="text" id="after" value="<?php echo htmlspecialchars(stripslashes(get_option('goodreads_after'))); ?>" size="10" />

          <em> e.g. &lt;li&gt;&lt;/li&gt;, &lt;p&gt;&lt;/p&gt;</em>

          </p>

        </td>

      </tr>

    </table>

    </fieldset>

    <p>

    <div class="submit">

      <input type="submit" name="configure_goodreads" value="<?php _e('Configure goodreads &raquo;', 'configure_goodreads') ?>" />

    </div>

    </p>

  </form>

</div>

<?php 

}



function goodreads_admin_menu() {

        if (function_exists('add_submenu_page')) {

                add_submenu_page('plugins.php', 'goodreads Configuration Page', 'Goodreads', 9, basename(__FILE__), 'goodreads_subpanel');

        }



}





add_action('admin_menu', 'goodreads_admin_menu'); 



